package ${filePackageName};

import cn.hutool.core.util.StrUtil;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 对象工具类
 *
 * @author ${projectAuthor}
 * @version 1.0
 * @date ${nowDate}
 */
public class BeanUtil {

    /**
     * bean转换map
     *
     * @param bean
     * @return
     */
    public static Map<String, Object> beanToMap(Object bean) {
        return beanToMap(bean, true);
    }

    /**
     * bean转换map
     *
     * @param bean
     * @return
     */
    public static Map<String, Object> beanToMap(Object bean, boolean ignoreEmptyValue) {
        Map<String, Object> map = new HashMap<>();
        if (bean == null) {
            return map;
        }
        map = cn.hutool.core.bean.BeanUtil.beanToMap(bean);
        Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> next = iterator.next();
            if (ignoreEmptyValue) {
                if (next.getValue() == null || StrUtil.isBlank(next.getValue().toString())) {
                    iterator.remove();
                }
            } else {
                if (next.getValue() == null) {
                    iterator.remove();
                }
            }
        }
        return map;
    }
}
